package com.ysxsoft.wdg.ui.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.TbkAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.CouponListResponse;
import com.ysxsoft.wdg.bean.response.CouponResponse;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.BigDecimalUtil;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.transform.FastSpeedLinearLayoutManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class CouponListActivity extends BaseActivity {
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_refresh)
    LinearLayout llRefresh;
    private int page = 1;
    private QuanAdapter adapter;
    private String account, companyid;//账户  店铺id

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coupon_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("可用优惠券");

        account = getIntent().getStringExtra("account");
        companyid = getIntent().getStringExtra("companyid");

        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setFocusable(false);
        recyclerview.setLayoutManager(new FastSpeedLinearLayoutManager(this));
        adapter = new QuanAdapter(R.layout.item_quan);
        recyclerview.setAdapter(adapter);

        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableAutoLoadmore(false);

        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getList();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
    }

    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    //获取列表
    private void getList() {
        OkHttpUtils.post()
                .url(UrlHost.SHOP_COUPON_LIST)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("companyid", companyid)
                .addParams("account", account)//	综合0，销量1，最新2，到手价倒序3，到手价正序4
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
//                    multStatusView.showError();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    CouponResponse c = JsonUtils.parseByGson(response, CouponResponse.class);
                    if (c == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(c.getCode())) {
                        List<CouponResponse.DataBean> list = c.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }
                } else {
//                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }


    class QuanAdapter extends BaseQuickAdapter<CouponResponse.DataBean, BaseViewHolder> {

        private int type = 0;

        public void setType(int type) {
            this.type = type;
            notifyDataSetChanged();
        }

        public QuanAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }


        @Override
        protected void convert(BaseViewHolder helper, CouponResponse.DataBean item) {
            int position = helper.getLayoutPosition();

            helper.setText(R.id.tv_money, item.getPrice());
            helper.setText(R.id.tv_shop, "仅限" + item.getCompany() + "店铺使用");
            helper.setText(R.id.tv_time, "有效时间至" + new SimpleDateFormat("yyyy-MM-dd").format(new Date(item.getEndtime() * 1000)).toString());
            helper.setText(R.id.tv_m_j, "满" + item.getLimit() + "元减" + item.getPrice());

            TextView tv2Use = helper.getView(R.id.tv_2_use);
            TextView tvUsed = helper.getView(R.id.tv_used);

            LinearLayout llR = helper.getView(R.id.ll_quan_r);
            LinearLayout llL = helper.getView(R.id.ll_quan_l);

            if (type != 2) {
                if (type == 0) {//未使用
                    tv2Use.setVisibility(View.VISIBLE);
                    tvUsed.setVisibility(View.GONE);
                } else if (type == 1) {//已使用
                    tv2Use.setVisibility(View.GONE);
                    tvUsed.setVisibility(View.VISIBLE);
                    tvUsed.setText("已使用");
                }
                llL.setBackgroundResource(position % 2 == 0 ? R.mipmap.quan_blue_l : R.mipmap.quan_red_l);
                llR.setBackgroundResource(position % 2 == 0 ? R.mipmap.quan_blue_r : R.mipmap.quan_red_r);
            } else {//过期
                tv2Use.setVisibility(View.GONE);
                tvUsed.setVisibility(View.VISIBLE);
                tvUsed.setText("已过期");

                llL.setBackgroundResource(R.mipmap.quan_gray_l);
                llR.setBackgroundResource(R.mipmap.quan_gray_r);
            }

            tv2Use.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("price", item.getPrice());
                    intent.putExtra("limit", item.getLimit());
                    intent.putExtra("couponId", item.getQuan_id());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
    }


}
